package com.ztjava.member.validator;

import java.util.regex.Pattern;

import com.jfinal.core.Controller;
import com.ztjava.core.base.BaseValidator;
import com.ztjava.member.model.MemberGroup;

public class RegisterValidator extends BaseValidator{
	@Override
	protected void validate(Controller c) {
		setShortCircuit(true);
		validateRequiredString("username",message, "请输入用户名!");
		validateString("username", 6, 30, message, "用户名必须6-30个字符!");
		Pattern pattern = Pattern.compile("[0-9]*");
		if(pattern.matcher(c.getPara("username")).matches()){
			addError(message, "用户名不能全是数字！");
		}
		validateRegex("username", "[0-9A-Za-z]*", message, "用户名必须由数字或字母组成!");
		validateRequiredString("password", message, "请输入密码！");
		validateString("password", 6, 30, message, "密码必须6-30个字符!");
		validateRequiredString("repassword", message, "请再次输入密码！");
		validateString("repassword", 6, 30, message, "再次输入密码必须6-30个字符!");
		validateEqualField("password", "repassword", message, "两次输入的密码不相同！");
		validateRequiredString("email", message, "请输入邮箱！");
		validateString("email", 4, 30, message, "邮箱必须3-50个字符!");
		validateEmail("email", message, "邮箱格式不正确！");
		
		Integer group_id = c.getParaToInt("group_id",0);
		if(group_id>0){
			MemberGroup memberGroup = MemberGroup.dao.findById(group_id);
			if(memberGroup==null){
				addError(message, "会员组不存在！");
			}
			if(memberGroup.getStr("group_id").equals("F")){
				addError(message, "此会员组不允许注册！");
			}
		}
	}

}
